package cn.com.gdut.ppj.user.service.impl;

import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.com.gdut.ppj.common.utils.UUIDFactory;
import cn.com.gdut.ppj.model.TbCandidateRecruitmentInfo;
import cn.com.gdut.ppj.model.TbCompany;
import cn.com.gdut.ppj.model.TbProjectExperience;
import cn.com.gdut.ppj.model.TbRecruitmentInformation;
import cn.com.gdut.ppj.model.TbResume;
import cn.com.gdut.ppj.model.TbSchoolStudyInfo;
import cn.com.gdut.ppj.model.TbUser;
import cn.com.gdut.ppj.model.TbUserResumeRef;
import cn.com.gdut.ppj.model.TbWorkExperience;
import cn.com.gdut.ppj.user.dao.IProjectExperienceDao;
import cn.com.gdut.ppj.user.dao.IRecruitmentInformationDao;
import cn.com.gdut.ppj.user.dao.IResumeDao;
import cn.com.gdut.ppj.user.dao.ISchoolStudyInfoDao;
import cn.com.gdut.ppj.user.dao.IUserDao;
import cn.com.gdut.ppj.user.dao.IUserResumeRefDao;
import cn.com.gdut.ppj.user.dao.IWorkExperienceDao;
import cn.com.gdut.ppj.user.service.IUserService;

@Service("userService")
public class UserServiceImpl implements IUserService {
	
	@Resource private IUserDao userDao;
	@Resource private IUserResumeRefDao userResumeRefDao;
	@Resource private IResumeDao resumeDao;
	@Resource private ISchoolStudyInfoDao schoolStudyInfoDao;
	@Resource private IWorkExperienceDao workExperienceDao;
	@Resource private IProjectExperienceDao projectExperienceDao;
	@Resource private IRecruitmentInformationDao riDao;
	
	@Override
	public TbUser register(TbUser user) {
		System.out.println("test result is true");
		
		TbUser tem = userDao.selectUserByEmail(user);
		if(null == tem){
			userDao.insertUser(user);
		}else{
			return null;
		}
		return user;
	}

	@Override
	public TbUser updateUser(TbUser user) {
		if( userDao.updateUser(user) > 0){
			return user;
		}
		return null;
	}

	@Override
	public TbResume addResume(TbUser user, TbResume resume) {
		TbUserResumeRef urr = new TbUserResumeRef();
		urr.setUrrId(UUIDFactory.getUUID());
		urr.setUserId(user.getUserId());
		urr.setResumeId(resume.getResumeId());
		Integer result = userResumeRefDao.insertUserResumeRef(urr);
		if(result > 0){
			result = resumeDao.insertResume(resume);
			if(result > 0){
				return resume;
			}
		}
		return null;
	}

	@Override
	public TbResume updateResume(TbUser user, TbResume resume) {
		Integer result = resumeDao.updateResumeById(resume);
		if(result > 0){
			return resume;
		}
		return null;
	}

	@Override
	public List<TbResume> selectResume(TbUser user, TbResume resume) {
		TbUserResumeRef urr = new TbUserResumeRef();
		urr.setUserId(user.getUserId());
		List<TbUserResumeRef> refs= userResumeRefDao.selectUserResumeRefBy(urr);
		if(null != refs && !refs.isEmpty()){
			urr = refs.get(0);
		}else{
			return null;
		}
		resume.setResumeId(urr.getResumeId());
		List<TbResume> resumes = resumeDao.selectResumeBy(resume);
		return resumes;
	}

	@Override
	public List<TbCompany> selectCompany(TbCompany company) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TbRecruitmentInformation> selectRecruitmentInformation(TbRecruitmentInformation tri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TbCandidateRecruitmentInfo addCandidateRecruitmentInfo(TbCandidateRecruitmentInfo tcri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TbCandidateRecruitmentInfo> selectCandidateRecruitmentInfo(TbCandidateRecruitmentInfo tcri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TbUser selectUser(TbUser user) throws Exception{
		TbUser tem = userDao.selectUserByEmail(user);
		if(null != tem){
			if(tem.getUserPassword().equals(user.getUserPassword())){
				tem.setUserLastLoginTime(Calendar.getInstance().getTime());
				userDao.updateUser(tem);
				return tem;
			}
		}
		return null;
	}

	@Override
	public TbWorkExperience selectWorkExperience(TbWorkExperience we) throws Exception {
		List<TbWorkExperience> list = workExperienceDao.selectWorkExperienceBy(we);
		if(null != list && !list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public TbWorkExperience updateWorkExperience(TbWorkExperience we) throws Exception {
		Integer result = 0;
		List<TbWorkExperience> list = workExperienceDao.selectWorkExperienceBy(we);
		if(null != list && !list.isEmpty()){
			we.setId(list.get(0).getId());
			result = workExperienceDao.updateWorkExperienceById(we);
		}else{
			we.setId(UUIDFactory.getUUID());
			result = workExperienceDao.insertWorkExperience(we);
		}
		if(result > 0){
			return we;
		}
		return null;
	}

	@Override
	public TbWorkExperience insertWorkExperience(TbWorkExperience we) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TbProjectExperience selectProjectExperience(TbProjectExperience pe) throws Exception {
		List<TbProjectExperience> list = projectExperienceDao.selectProjectExperience(pe);
		if(null != list && !list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public TbProjectExperience updateProjectExperience(TbProjectExperience pe) throws Exception {
		Integer result = 0;
		List<TbProjectExperience> list = projectExperienceDao.selectProjectExperience(pe);
		if(null != list && !list.isEmpty()){
			pe.setId(list.get(0).getId());
			result = projectExperienceDao.updateTbProjectExperienceById(pe);
		}else{
			pe.setId(UUIDFactory.getUUID());
			result = projectExperienceDao.insertProjectExperience(pe);
		}
		if(result > 0){
			return pe;
		}
		return null;
	}

	@Override
	public TbProjectExperience insertProjectExperience(TbProjectExperience pe) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TbSchoolStudyInfo selectSchoolStudyInfo(TbSchoolStudyInfo ssi) throws Exception {
		List<TbSchoolStudyInfo> list = schoolStudyInfoDao.selectTbSchoolStudyInfoBy(ssi);
		if(null != list && !list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public TbSchoolStudyInfo updateSchoolStudyInfo(TbSchoolStudyInfo ssi) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TbSchoolStudyInfo insertSchoolStudyInfo(TbSchoolStudyInfo ssi) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TbRecruitmentInformation> selectRecruitmentInformationByCandidate(TbCandidateRecruitmentInfo cri)
			throws Exception {
		List<TbRecruitmentInformation> list = riDao.selectRecruitmentInformationByCandidate(cri);
		return list;
	}

}
